//
//  UIColor+Add.m
//  HDDProject
//
//  Created by 神廷 on 2020/9/27.
//

#import "UIColor+Add.h"

@implementation UIColor (Add)


+ (UIColor *)appColor {
    return [UIColor appBlueLabelColor];
}

+ (UIColor *)appBGColor {
    return [UIColor colorWithHexString:@"#F2F4F5"];
}

//导航颜色
+ (UIColor *)appNavTitleColor {
    return [UIColor appWhiteLabelColor];
}

//黑色字体颜色
+ (UIColor *)appBlackLabelColor {
    return [UIColor colorWithHexString:@"#333333"];
}

//黑色字体颜色
+ (UIColor *)appBlack333LabelColor {
    return [UIColor colorWithHexString:@"#333333"];
}

//蓝色字体颜色
+ (UIColor *)appBlueLabelColor {
    return [UIColor colorWithHexString:@"#0894FF"];
}

//绿色字体颜色
+ (UIColor *)appGreenColor {
    return [UIColor colorWithHexString:@"#3EA700"];
}

//红色字体颜色
+ (UIColor *)appRedLabelColor {
    return [UIColor colorWithHexString:@"#F9301B"];
}

//灰色字体颜色
+ (UIColor *)appGrayLabelColor {
    return [UIColor colorWithHexString:@"#999999"];
}

//灰色字体颜色
+ (UIColor *)appGrayB3LabelColor {
    return [UIColor colorWithHexString:@"#B3B3B3"];
}

//A1灰色字体颜色
+ (UIColor *)appGrayA1LabelColor {
    return [UIColor colorWithHexString:@"#A1A1A1"];
}

//F6灰色字体颜色
+ (UIColor *)appGrayF6LabelColor {
    return [UIColor colorWithHexString:@"#F6F6F6"];
}

//666666灰色字体颜色
+ (UIColor *)appGray666LabelColor {
    return [UIColor colorWithHexString:@"#666666"];
}

//白色字体颜色
+ (UIColor *)appWhiteLabelColor {
    return [UIColor colorWithHexString:@"#FFFFFF"];
}

//橙色字体颜色
+ (UIColor *)appOrangeLabelColor {
    return [UIColor colorWithHexString:@"#FF6200"];
}


//+ (UIColor *)colorWithHexString:(NSString *)color {
//    if ([color hasPrefix:@"0X"]) {
//        color = [color substringFromIndex:2];
//    }
//    
//    if ([color hasPrefix:@"#"]) {
//        color = [color substringFromIndex:1];
//    }
//    
//    if([color length] == 8) {
//        NSRange range;
//        range.location = 0;
//        range.length = 2;
//        
//        NSString *aString = [color substringWithRange:range];
//        unsigned int a;
//        [[NSScanner scannerWithString:aString] scanHexInt:&a];
//        float alpha = (float)a / 255.0f;
//        color = [color substringFromIndex:2];
//        return [self colorWithHexString:color alpha:alpha];
//    }
//    
//    else {
//        return [self colorWithHexString:color alpha:1.0f];
//    }
//}


+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha {
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6) {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

+ (void)configGradient:(UIView *)view StartPoint:(CGPoint)startPoint EndPoint:(CGPoint)endPoint  FirstColor:(UIColor *)firstColor SecondColor:(UIColor *)secondColor {
    CAGradientLayer * gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = view.bounds;
    gradientLayer.colors = @[(__bridge id)firstColor.CGColor,(__bridge id)secondColor.CGColor];
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    gradientLayer.locations = @[@0,@1];
    [view.layer insertSublayer:gradientLayer atIndex:0];
}

+ (instancetype)due_colorGradientChangeWithSize:(CGSize)size
                                     direction:(DUEGradientChangeDirection)direction
                                    startColor:(UIColor *)startcolor
                                      endColor:(UIColor *)endColor {
    if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
        return nil;
    }
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    
    CGPoint startPoint = CGPointZero;
    if (direction == DUEGradientChangeDirectionDownDiagonalLine) {
        startPoint = CGPointMake(0.0, 1.0);
    }
    gradientLayer.startPoint = startPoint;
    
    CGPoint endPoint = CGPointZero;
    switch (direction) {
        case DUEGradientChangeDirectionLevel:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        case DUEGradientChangeDirectionVertical:
            endPoint = CGPointMake(0.0, 1.0);
            break;
        case DUEGradientChangeDirectionUpwardDiagonalLine:
            endPoint = CGPointMake(1.0, 1.0);
            break;
        case DUEGradientChangeDirectionDownDiagonalLine:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        default:
            break;
    }
    gradientLayer.endPoint = endPoint;
    
    gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:image];
}

@end
